﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBQLTV;

namespace DAO
{
    public class CTPhieuTraSachDAO
    {
        public bool InsertCTPhieuTraSach(int MaPhieuTS, int MaCuonSach, string NgayMuon, int SoNgayMuon, int TienPhat)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DateTime ngMuon = Convert.ToDateTime(NgayMuon);
                CTPHIEUTRA phieuTra = new CTPHIEUTRA()
                {
                    MaPhieuTra = MaPhieuTS,
                    MaCuonSach = MaCuonSach,
                    NgayMuon = ngMuon,
                    SoNgayMuon = SoNgayMuon,
                    TienPhat = TienPhat
                };
                CUONSACH cuonsach = entity.CUONSACHes.FirstOrDefault(o => o.MaCuonSach == MaCuonSach);
                cuonsach.TinhTrang = new CuonSachDAO().TT_TrongTV;
                cuonsach.MaDocGia = null;
                entity.CTPHIEUTRAs.AddObject(phieuTra);
                entity.SaveChanges();
                return true;
            }
        }
        public bool DeleteCTPhieuTraSach(int MaPhieuTra)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                var listCT = from temp in entity.CTPHIEUTRAs where temp.MaPhieuTra == MaPhieuTra select temp;
                if (listCT.Count() == 0) return false;
                foreach (CTPHIEUTRA ct in listCT)
                {
                    entity.CTPHIEUTRAs.DeleteObject(ct);
                }
                entity.SaveChanges();
                return true;
            }
        }
        public List<CTPHIEUTRA> GetListCTPhieuTra(int MaCuonSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                var list = from temp in entity.CTPHIEUTRAs where temp.MaCuonSach == MaCuonSach select temp;
                return list.ToList();
            }
        }
    }
}
